package cn.exrick.xboot.banner.controller;


import cn.exrick.xboot.banner.entity.Banner;
import cn.exrick.xboot.banner.service.IBannerService;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.config.custom.CustomPageUtils;
import cn.exrick.xboot.modules.content.entity.HontUser;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.alibaba.fastjson.JSON;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yck
 * @since 2022-11-24
 */
@RestController
@RequestMapping("/xboot/banner")
public class BannerController {
    private final Logger logger = LoggerFactory.getLogger(BannerController.class);

    @Autowired
    public IBannerService iBannerService;
    @Autowired
    private SecurityUtil securityUtil;

    /**
     * 分页查询数据
     *
     * @param page   分页信息
     * @param banner 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getBannerPageList")
    public Result<Object> getBannerPageList(Page<Banner> page, Banner banner) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iBannerService.selectPage(page, banner)));
    }

    /**
     * 保存和修改公用的
     *
     * @param banner 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/bannerSave")
    public Result<Object> bannerSave(@RequestBody Banner banner) {
        if (StringUtils.isBlank(banner.getId())) {

            HontUser u = securityUtil.getCurrUser();
            banner.setCreateBy(u.getNickName());
        }
        iBannerService.saveOrUpdate(banner);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/bannerGetById")
    public Result<Object> bannerGetById(String id) {
        Banner byId = iBannerService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/bannerDelete")
    public Result<Object> hontUserDelete(String id) {
        Banner byId = iBannerService.getById(id);
        byId.setDel(true);
        iBannerService.updateById(byId);
        return ResultUtil.success("删除成功");
    }

}
